<template>
  <div>
      <div style="margin-bottom: 5px">
          <el-input v-model="title"  placeholder="请输入活动" suffix-icon="el-icon-search" style="width: 200px"
          @keyup.enter.native="loadPost"></el-input>
 
          <el-select v-model="status" placeholder="请选择评选状态">
           <el-option
            v-for="item in statuss"
            :key="item.value"
            :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
       
     
          <el-button type="primary" style="margin-left: 10px" @click="loadPost">查询</el-button>
          <el-button type="success" @click="resetParam">重置</el-button>
          <el-button type="primary" style="margin-left: 10px" @click="add">新增</el-button>
      </div>
  <el-table :data="tableData"
  :header-cell-style="{background:'#f2f5fc',color:'#555'}" border>
      <el-table-column prop="id" label="ID" width="160">
      </el-table-column>

      <el-table-column prop="title" label="标题" width="160">
      </el-table-column>
 
      <el-table-column prop="enrollBegin" label="报名开始"  width="160">
      </el-table-column>

      <el-table-column prop="enrollEnd" label="报名结束"  width="160">
      </el-table-column>

      
      <el-table-column prop="type" label="活动类型"  width="60" :formatter="typeFormate">
      </el-table-column>

      <el-table-column prop="address" label="活动地址"  width="160">
      </el-table-column>

      <el-table-column prop="organizer" label="举办方"  width="160">
      </el-table-column>
     
      <el-table-column prop="status" label="状态"  width="60" :formatter="statusFormate">
      </el-table-column>

      <el-table-column prop="filePath" label="附件"  width="160">
      </el-table-column>

      <el-table-column prop="createUserName" label="创建人姓名"  width="160">
      </el-table-column>
      <el-table-column prop="createTime" label="创建时间"  width="160">
      </el-table-column>

      
      <el-table-column prop="operate" label="操作" width="200">
          <template slot-scope="scope">
          <el-button size="small" type="success" @click="mod(scope.row.id)">编辑</el-button>
          <el-button size="small" type="success" @click="dowm(scope.row)">下载附件</el-button>

              
          <el-popconfirm
                      style="margin-left: 5px;"
                      title="这是一段内容确定删除吗？"
                      @confirm="del(scope.row.id)"
              >
                  <el-button slot="reference" size="small" type="danger">删除</el-button>
              </el-popconfirm>
              <el-button  size="small" type="danger"  style="margin-left: 5px;" @click="unreleased(4,scope.row.id)">下架</el-button>
              <el-button  size="small" type="primary"  style="margin-left: 5px;" @click="unreleased(3,scope.row.id)">发布</el-button>
          </template>
      </el-table-column>

  </el-table>
      <el-pagination
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
              :current-page="pagenum"
              :page-sizes="[2, 5, 10, 20]"
              :page-size="pageSize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total">
      </el-pagination>

      <el-dialog
              title="提示"
              :visible.sync="centerDialogVisible"
              width="80%"
              center>
<!--           //增加表单-->
          <el-form :model="form" :rules="rules" ref="form" label-width="100px" class="demo-ruleForm">
          
              <el-form-item label="标题" prop="title">
                      <el-input  v-model="form.title"></el-input>
              </el-form-item>

              <el-form-item label="简介" prop="brief">
                      <el-input  v-model="form.brief"></el-input>
              </el-form-item>


              <el-form-item label="内容" prop="content">
                <WangEditor @sendEditor="sendEditor" ref="myEditor"></WangEditor>
              </el-form-item>

       

                 <el-form-item label="报名开始时间" prop="enrollBegin">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.enrollBegin" style="width: 100%;"></el-date-picker>           
                 </el-form-item>

                 
                 <el-form-item label="报名结束时间" prop="enrollEnd">
                <el-date-picker type="date" placeholder="选择日期" v-model="form.enrollEnd" style="width: 100%;"></el-date-picker>           
                 </el-form-item>


             
                      <el-form-item label="活动类型">
                  <el-select v-model="form.type" placeholder="请选择活动类型">
                  <el-option label="线上" value="1"></el-option>
                  <el-option label="线下" value="2"></el-option>
                   </el-select>
                </el-form-item>


                <el-form-item label="活动地址" prop="address">
                      <el-input v-model="form.address"></el-input>
              </el-form-item>

              <el-form-item label="举办方" prop="organizer">
                      <el-input v-model="form.organizer"></el-input>
              </el-form-item>
      

              <el-form-item label="附件" prop="filePath">
                <!-- 上传文件 -->
            
                   <el-upload
                     class="upload-demo"
                     action="http://localhost:9001/jk/admin/activity/uploadeditor"
                    :data={id:id}
                    :on-change="handleChange"
                    :limit="1"
                    :file-list="fileList">
            <el-button size="small" type="primary">点击上传</el-button>
            <span style="margin-left:20px">{{form.filePath}}</span>
            </el-upload>
              </el-form-item>
          </el-form>

          <span slot="footer" class="dialog-footer">
            <el-button @click="centerDialogVisible = false">取 消</el-button>
           <el-button type="primary" @click="save">确 定</el-button>
            </span>
      </el-dialog>
  </div>
</template>

<script>
import WangEditor from '@/views/editor/wangEditor.vue'
    export default {
        components:{
            WangEditor
        },
        name: "ActivityList",
        data() {
         
            return {
                // 查询下拉框
                statuss: [{
          value: -1,
          label: '全部'
        }, {
          value: 1,
          label: '筹划中'
        }, {
          value: 2,
          label: '已发布'
        },{
          value: 3,
          label: '已下架'
        }],
        status: -1,
        title:'',
       
        id:'',
              tableData: [],
              pagenum:1,
              pageSize:2,
              total:0,
              MStype:'',
              createUserName:'',
              fileList:[],
                centerDialogVisible:false,
                form:{
                    title:'',
                    content:'',
                    organizer:'',
                    filePath:'',
                    enrollBegin:'',
                    enrollEnd:'',
                    brief:'',
                    type:'',
                    address:''

                },
                rules: {
                   
                    title: [
                        { required: true, message: '请输入名字', trigger: 'blur' },
                    ],
                   

                }

            }

        },
        created(){
            this.session = JSON.parse(sessionStorage.getItem('CurUser'))
            this.form.createUserId=this.session.createUserId
            this.form.createUserName=this.session.createUserName
           
        },
        methods:{
            sendEditor(val){
                this.form.content = val.replace(/<[^>]+>/g,"")
      
            },
            // 类型
            typeFormate (row, index){
                if(row.type == 1){
                    return "线上";
                }else if(row.type == 2){
                    return "线下";
                }
            },
            // 显示状态
            statusFormate(row, index){
                if(row.status == 1){
                    return "筹划中";
                }else if(row.status == 2){
                    return "已发布";
                }else if(row.status == 3){
                    return "已下架";
                }
            },
            resetForm() {
                this.$refs.form.resetFields();
                this.$refs.myEditor.editor.txt.html("")
            },
         
            mod(id){
              
              this.$axios.get(this.$httpUrl+'/jk/admin/activity/edit?id='+id).then(res=>res.data).then(res=>{
                this.MStype=2
                  if (res.resCode=="000000"){
                    this.centerDialogVisible=true
                    this.$nextTick(()=>{
                        this.id=res.data.id
                    this.form.id = res.data.id
                    this.form.title = res.data.title
                    this.form.content = res.data.content
                    this.form.brief = res.data.brief
                    this.form.enrollBegin = res.data.enrollBegin
                    this.form.enrollEnd = res.data.enrollEnd
                    this.form.type = res.data.type
                    this.form.address = res.data.address
                    this.form.organizer = res.data.organizer
                  this.$refs.myEditor.editor.txt.html(res.data.content)
       
              })    
                  }else{
                  this.$message({
                  message: '操作失败',
                  type: 'error'
              });
                      }
                  })
          },
            // 删除
            del(id){
                this.$axios.delete(this.$httpUrl+'/jk/admin/activity/remove?id='+id).then(res=>res.data).then(res=>{

                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面
                        this.loadPost()
                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })

            },
            add(){
  
                this.centerDialogVisible=true
                this.MStype=1
                this.$nextTick(()=>{
                  this.resetForm()
                //    这个在第一次的时候不能使用，所以需要nextTick异步
                 })
                },
          
            // 修改
            doMod(){
                this.$axios.put(this.$httpUrl+'/jk/admin/activity/update',
                    this.form

                ).then(res=>res.data).then(res=>{
                    this.MStype=2
                    if (res.resCode=='000000'){
                        this.$message({
                            message: '操作成功',
                            type: 'success'
                        });
                        //成功之后离开刷新页面

                        this.centerDialogVisible=false
                        this.loadPost()
                        // this.resetForm()

                    }else{
                        this.$message({
                            message: '操作失败',
                            type: 'error'
                        });
                    }

                })
            },
       
              // 增加保存
              doSave(){
                  this.$axios.post(this.$httpUrl+'/jk/admin/activity/add',
                      this.form
  
                  ).then(res=>res.data).then(res=>{
                   
                      if (res.resCode=='000000'){
                          this.$message({
                              message: '操作成功',
                              type: 'success'
                          });
                          //成功之后离开刷新页面
  
                          this.centerDialogVisible=false
                          this.loadPost()
                          // this.resetForm()
  
                      }else{
                          this.$message({
                              message: '操作失败',
                              type: 'error'
                          });
                      }
  
                  })
              },
              save(){
                  this.$refs.form.validate((valid) => {
                      if (valid) {
                        if (this.MStype==2){
                            
                            this.doMod()
                        }else{
                           
                            this.doSave()
                        }
  
                      } else {
                          console.log('error submit!!');
                          return false;
                      }
                  });
  
              },
            handleSizeChange(val) {
                
                this.pagenum=1
                this.pageSize=val
                this.loadPost()
            },
            handleCurrentChange(val) {
                
                this.pagenum=val
                this.loadPost()
            },
            handleChange(file, fileList) {
                this.form.filePath=file.response
                var a=""+this.form.filePath
                this.form.id = a.slice(11,30)
                console.log(this.form.id)
                this.fileList = fileList.slice(-3);
            },
          
            resetParam(){
                this.title='',
                this.status=-1
    
                
            },
            loadPost(){
             
             this.$axios.get(this.$httpUrl+'/jk/admin/activity/list?status='+this.status+'&title='+this.title+'&pagenum='+this.pagenum +'&pageSize='+this.pageSize
             ).then(res=>res.data).then(res=>{
          
                 if (res.resCode=='000000'){
                     this.tableData=res.data.records
                     this.total=res.data.total

                 }else{
                     alert('获取数据失败')
                 }

             })
         },
            // 下架
        unreleased(status,id)
            { this.$axios.put(this.$httpUrl+'/jk/admin/activity/updateStatus?status='+status+'&id='+id).then(res=>res.data).then(res=>{
                console.log(res)
                if (res.resCode=="000000"){
                          this.$message({
                              message: '操作成功',
                              type: 'success'
                          });                                
                      }else{
                          this.$message({
                              message: '操作失败',
                              type: 'error'
                          });
                      }

                })

            },
            dowm(row){
                console.log(row.filePath)
                if(row.filePath!='-'){ window.location = "http://localhost:9001/jk/admin/activity/down?id=" + row.id;}
               else{

                this.$message({
                              message: '没有附件',
                              type: 'error'
                          });
               }
            
            },
        },
        beforeMount() {
            
            this.loadPost();

        }
    }
</script>

<style>

</style>